package com.uibase.card
{
	import com.uibase.card.func.ICardClickControl;
	
	import flash.display.Sprite;
	
	public class CardGrid extends Sprite
	{
		private var _cards:Array;
		private var _cardvals:Array;
		
		private var _cardRotation:int;
		
		private var _iCardClickCtrlCls:Class;
		static private const CRAD_GAPS_X:int = 20;
		
		public function CardGrid()
		{
			super();
		}
		
		public function set cardClickControl(iCardClickCtrlCls:Class):void
		{
			_iCardClickCtrlCls = iCardClickCtrlCls;
			
			var card:Card;
			var i:int;
			for(i=0; i<_cards.length; i++)
			{
				card = _cards[i] as Card;
				if(card)
				{
					if(_iCardClickCtrlCls)
					{
						var cardClickCtrl:ICardClickControl = new _iCardClickCtrlCls as ICardClickControl;
						if(cardClickCtrl)
						{
							card.cardClickControl = cardClickCtrl;
						}
					}
				}
			}
		}
		
		public function destroy():void
		{
			_cardvals = null;
			if(!_cards)
			{
				return;
			}
			
			var i:int;
			var card:Card;
			for(i=0; i<_cards.length; i++)
			{
				card = _cards[i] as Card;
				if(card)
				{
					card.destroy();
					removeChild(card);
				}
			}
			
			_cards    = null;
		}
		
		public function updateCards(cards:Array):void
		{
			var i:int;
			var card:Card;
			var cardVal:int;
			
			destroy();
			//_cardvals = cards;
			if(!cards)
			{
				return;
			}
			
			_cards    = [];
			for(i=0; i<cards.length; i++)
			{
				cardVal = cards[i] as int;
				addCard(cardVal);
			}
		}
		
		public function addCard(cardVal:int):void
		{
			var card:Card;
			
			if(!_cardvals)
			{
				_cardvals = [];
			}
			
			if(!_cards)
			{
				_cards = [];
			}
			
			_cardvals.push(cardVal);
			card = new Card(cardVal);
			if(_iCardClickCtrlCls)
			{
				var cardClickCtrl:ICardClickControl = new _iCardClickCtrlCls as ICardClickControl;
				if(cardClickCtrl)
				{
					card.cardClickControl = cardClickCtrl;
				}
			}
			
			card.x = (_cardvals.length-1)*CRAD_GAPS_X;
			card.index = _cardvals.length-1;
			addChild(card);
			_cards.push(card);
		}
		
		public function delCardByVal(cardVal:int):void
		{
			var arrTmp:Array = [];
			var i:int;
			var val:int;
			var isDel:Boolean = false;
			
			if(!_cardvals || _cardvals.length==0)
			{
				return;
			}
			
			for(i=0; i<_cardvals.length; i++)
			{
				val = _cardvals[i] as int;
				if(val == cardVal && !isDel)
				{
					isDel = true;
					continue;
				}
				arrTmp.push(_cardvals[i]);
			}
			
			updateCards(arrTmp);
		}
		
		public function getCardByIndex(index:int):Card
		{
			var card:Card;
			
			if(!_cards)
			{
				return null;
			}
			
			card = _cards[index] as Card;
			return card;
		}
		
		public function set cardRotation(rotationY:int):void
		{
			var card:Card;
			var i:int;
			
			_cardRotation = rotationY;
			for(i=0; i<_cards.length; i++)
			{
				card = _cards[i] as Card;
				if(card)
				{
					card.cardRotation = _cardRotation;
				}
			}
		}
		
		public function get cardRotation():int
		{
			return _cardRotation;
		}
	}
}